package com.example.mytayunapi.Controller;

import com.example.mytayunapi.Entity.LoginLogsInfo;
import com.example.mytayunapi.Entity.LoginResponse;
import com.example.mytayunapi.Entity.adminInfo;
import com.example.mytayunapi.Entity.userInfo;
import com.example.mytayunapi.Service.LoginlogsInfoService;
import com.example.mytayunapi.Service.TokenService;
import com.example.mytayunapi.Service.userInfoService;
import com.example.mytayunapi.Vo.UserProduct;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.example.mytayunapi.util.UserAgentParser.*;

@CrossOrigin
@RestController
@RequestMapping("query")
public class userController {
    @Autowired
    private userInfoService userService;

    // 确保使用@Autowired来注入TokenService
    @Autowired
    private TokenService tokenService;

    @Autowired
    private LoginlogsInfoService loginLogsService; // 登录日志服务

    @Autowired
    private HttpServletRequest request; // 用于获取请求信息

    //http://localhost:8080/query/aurelo
    @GetMapping("/aurelo")
    public List<userInfo> aurelo(@RequestParam String School_degree){
        return userService.getNickname(School_degree);
    }

    @GetMapping ("/user")
    public List<userInfo> user(){
        return userService.getUserInfo();
    }


    @GetMapping("/page")
    public   PageInfo<userInfo> pageInfo(@RequestParam Integer page, @RequestParam Integer size){
      return userService.getPageInfo(page,size);
    }


    //http://localhost:8080/query/users        get
    @GetMapping("/users")
    public List<userInfo> users( String School_degree){
        return userService.getSchool_degree(School_degree);
    }

    //修改用户信息
    //http://localhost:8080/query/update
    @PostMapping("/update")
    public Integer update(userInfo userinfo){
        return userService.updateUserInfo(userinfo);
}



     //插入用户信息
    //http://localhost:8080/query/insert
    @PostMapping("/insert")
    public Integer insert(userInfo userinfo){
        return userService.insertUserInfo(userinfo);
    }

    //判断用户登录信息
    //http://localhost:8080/query/login

    @PostMapping("/login")
    public ResponseEntity<?> login(HttpServletRequest request, @RequestParam String School_degree, @RequestParam String password) {
        // 从 HttpServletRequest 中获取IP地址和User-Agent
        String Login_ip = request.getRemoteAddr();
        String Login_web = request.getHeader("User-Agent");

        // 解析User-Agent字符串以提取浏览器和操作系统信息
        String browser = parseUserAgentForBrowser(Login_web);
        String os = parseOSVersionAndArchitecture(Login_web);

        // 创建登录日志对象
        LoginLogsInfo loginLogsinfo = new LoginLogsInfo();
        loginLogsinfo.setSchool_degree(School_degree);
        loginLogsinfo.setLogin_ip(Login_ip);
        loginLogsinfo.setLogin_web(Login_web);
        loginLogsinfo.setLogin_time(new Timestamp(new Date().getTime()));
        loginLogsinfo.setBrowser(browser);
        loginLogsinfo.setOs(os);

        // 检查登录信息是否已填写
        if (School_degree == null || password == null) {
            return new ResponseEntity<>(new LoginResponse("未填写登录信息", false, null), HttpStatus.BAD_REQUEST);
        }

        // 调用服务层方法验证用户
        userInfo user = userService.getUserdegreeApass(School_degree, password);
        if (user != null && user.getLogin_1()) { // 检查用户是否存在且是否激活
            // 用户存在且激活，继续生成token的逻辑
            String token = tokenService.generateToken(user);
            if (token != null) {
                // Token生成成功
                LoginResponse loginResponse = new LoginResponse("登录成功", true, token);
                loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
                return new ResponseEntity<>(loginResponse, HttpStatus.OK);
            } else {
                // Token生成失败
                loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
                return new ResponseEntity<>(new LoginResponse("Token生成失败", false, null), HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } else {
            // 用户不存在或账户未激活
            loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
            return new ResponseEntity<>(new LoginResponse("账户未激活或不存在", false, null), HttpStatus.FORBIDDEN);
        }
    }





//    @PostMapping("/login")
//    public ResponseEntity<?> login(HttpServletRequest request,@RequestParam String School_degree, @RequestParam String password) {
//
//        // 从 HttpServletRequest 中获取IP地址和User-Agent
//        String Login_ip = request.getRemoteAddr();
//        String Login_web = request.getHeader("User-Agent");
//
//        // 解析User-Agent字符串以提取浏览器和操作系统信息
//        String browser = parseUserAgentForBrowser(Login_web);
//        String os = parseOSVersionAndArchitecture(Login_web);
//
//        // 创建登录日志对象
//        LoginLogsInfo loginLogsinfo = new LoginLogsInfo();
//        loginLogsinfo.setSchool_degree(School_degree);
//        loginLogsinfo.setLogin_ip(Login_ip);
//        loginLogsinfo.setLogin_web(Login_web);
//        loginLogsinfo.setLogin_time(new Timestamp(new Date().getTime()));
//        loginLogsinfo.setBrowser(browser); // 假设LoginLogsInfo有一个设置浏览器的setter方法
//        loginLogsinfo.setOs(os); // 假设LoginLogsInfo有一个设置操作系统的setter方法
//
//
//
//
//        if (School_degree == null || password == null) {
//            return new ResponseEntity<>(new LoginResponse("未填写登录信息", false, null), HttpStatus.BAD_REQUEST);
//        }
//
//        userInfo user = userService.getUserdegreeApass(School_degree, password);
//        if (user != null) {
////            LoginResponse loginResponse = new LoginResponse("登录成功", true);
////            return new ResponseEntity<>(loginResponse, HttpStatus.OK);
//
//            String token = tokenService.generateToken(user); // 假设user对象有方法提供JWT主题
//            if (token != null) {
//                LoginResponse loginResponse = new LoginResponse("登录成功", true, token);
//
//                System.out.println("Browser: " + browser);
//                System.out.println("OS: " + os);
//
//                loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
//
//                return new ResponseEntity<>(loginResponse, HttpStatus.OK);
//            } else {
//
//                loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
//
//                // 如果Token生成失败，返回错误响应
//                return new ResponseEntity<>(new LoginResponse("Token生成失败", false, null), HttpStatus.INTERNAL_SERVER_ERROR);
//
//
//            }
//        } else {
//            loginLogsService.saveLoginLog(loginLogsinfo); // 保存登录日志
//
//            return new ResponseEntity<>(new LoginResponse("登录失败", false), HttpStatus.UNAUTHORIZED);
//        }
//    }


    //根据用户登录的学号获取用户信息
    //http://localhost:8080/query/getdegree
    @GetMapping("/getdegree")
    public List<userInfo> getdegree(String School_degree){
        return userService.getUserdegree(School_degree);

    }

    //根据用户登录的学号获取用户信息并修改用户信息
    //http://localhost:8080/query/change
    @PostMapping("/change")
    public Integer change(userInfo userinfo){
        System.out.println(userinfo);
        return userService.changeUserInfo(userinfo);
    }



    //根据用户登录的学号冻结用户的账户信息
    //http://localhost:8080/query/alter
    @PostMapping("/alter")
    public Integer alter(userInfo userinfo){
        return userService.changeUserInfoLogin(userinfo);
    }


    //根据用户要求修改密码
    //http://localhost:8080/query/alterpassword
    @PostMapping("/alterpassword")
    public Integer alterpassword(userInfo userinfo){
        return userService.changeUserInfoPassword(userinfo);
    }

    //查看用户绑定的学号
    //http://localhost:8080/query/findSchool_degree
    @GetMapping("/findSchool_degree")
    public String findSchool_degree(String School_degree){
        return userService.getSchool_number(School_degree);
    }

    //查看用户绑定的姓名
    //http://localhost:8080/query/find_name
    @GetMapping("/find_name")
    public String find_name(String School_degree){
        return userService.getSchool_name(School_degree);
    }

    //用户可以绑定姓名
    //http://localhost:8080/query/altername
    @PostMapping("/altername")
    public Integer altername(userInfo userinfo){
        return userService.updateUserInfoName(userinfo);
    }

    //可以通过传过来的商品编号查到卖家信息
    //http://localhost:8080/query/maijaixuehao
    @GetMapping("/maijaixuehao")
    public List<UserProduct> maijaixuehao(Integer Product_id){
        return userService.getmaijiaxuehao(Product_id);

    }

    //http://localhost:8080/query/change1
    @PostMapping("/change1")
    public Integer change1(userInfo userinfo){
        return userService.updateUsersInfoName(userinfo);
    }

    //http://localhost:8080/query/change2
    @PostMapping("/change2")
    public Integer change2(userInfo userinfo){
        return userService.updateUsersQXInfoName(userinfo);
    }

    //根据学号获得用户信息
    //http://localhost:8080/query/getyong
    @GetMapping("/getyong")
    public List<userInfo> getyong(String School_degree){
        return userService.getyonghu(School_degree);
    }

}



